﻿using GalaSoft.MvvmLight.CommandWpf;
using Mimi.Common;
using Mimi.Models.DataModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mimi.ViewModels
{
    public class MainWindowViewModel: BindingBase, IMainWindowViewModel
    {
        #region Fields
        IExpenseViewModel expenseViewModel;
        IBudgetViewModel budgetViewModel;
        IStorageManager storageManager;
        IOverviewViewModel overviewViewModel;
        #endregion

        #region Properties

        #endregion

        #region Commands
        private RelayCommand loadStoredDataCommand;
        /// <summary>
        /// <see cref="ButtonClickedCommand"/>
        /// </summary>
        public RelayCommand LoadStoredDataCommand
        {
            get { return loadStoredDataCommand ?? (loadStoredDataCommand = new RelayCommand(LoadData)); }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="expenseViewModel"></param>
        /// <param name="budgetViewModel"></param>
        public MainWindowViewModel(IExpenseViewModel expenseViewModel, IBudgetViewModel budgetViewModel, IStorageManager storageManager,
            IOverviewViewModel overviewViewModel)
        {
            this.expenseViewModel = expenseViewModel;
            this.budgetViewModel = budgetViewModel;
            this.storageManager = storageManager;
            this.overviewViewModel = overviewViewModel;
        }

        /// <summary>
        /// <see cref="LoadData"/>
        /// </summary>
        public void LoadData()
        {
            if (!File.Exists("ExpenseManager.sqlite"))
            {
                //Create database
                storageManager.CreateStorageDatabase();

                //Create budget table
                var budgetPropertyArray = typeof(Purpose).GetProperties();
                storageManager.CreateBudgetTable(budgetPropertyArray.Select((x) => x.Name));

                //Create expense table
                var expensePropertyArray = typeof(Expense).GetProperties();
                storageManager.CreateExpenseTable(expensePropertyArray.Select((x) => x.Name));

                //Create overview table
                var overviewPropertyArray = typeof(Overview).GetProperties();
                storageManager.CreateOverviewTable(overviewPropertyArray.Select((x) => x.Name));
                storageManager.AddOverviewData(new Overview() { OverviewIdentity = "0001", TotalBalance = "0" });

                //Create income table
                var incomePropertyArray = typeof(Income).GetProperties();
                storageManager.CreateIncomeTable(incomePropertyArray.Select((x) => x.Name));
            }
            
            //Init overview
            overviewViewModel.InitOverviewData();

            //Load income data
            overviewViewModel.LoadIncomeData();

            //Load budget
            budgetViewModel.LoadStoredData();

            //Load expense
            expenseViewModel.LoadStoredData();
        }
        #endregion
    }
}
